#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_clicked()
{
    QMessageBox* messageBox = new QMessageBox(this);

    messageBox->setWindowTitle("消息对话框");
    messageBox->setIcon(QMessageBox::Information);
    messageBox->setText("这是一个提示消息");
    messageBox->setStandardButtons(QMessageBox::Ok | QMessageBox::Save | QMessageBox::Cancel);

    // 自定义按钮，关联槽函数进行操作，标准按钮中，无法进行信号和槽的连接
//    QPushButton* bt = new QPushButton("确定");
//    messageBox->addButton(bt, QMessageBox::AcceptRole);
//    connect(bt, &QPushButton::clicked, this, &MainWindow::handle);

    messageBox->setAttribute(Qt::WA_DeleteOnClose);
    int statusCode = messageBox->exec();

    if(statusCode == QMessageBox::Ok)
    {
        qDebug() << "Ok";
    }
    else if(statusCode == QMessageBox::Save)
    {
        qDebug() << "Save";
    }
    else if(statusCode == QMessageBox::Cancel)
    {
        qDebug() << "Cancel";
    }
}

void MainWindow::handle()
{
    qDebug() << "对话框关闭";
}

void MainWindow::on_pushButton_2_clicked()
{
    int statusCode = QMessageBox::question(this, "问题提示消息对话框", "选择确认或关闭", QMessageBox::Ok | QMessageBox::Close);
    if(statusCode == QMessageBox::Ok)
    {
        qDebug() << "Ok";
    }
    else if(statusCode == QMessageBox::Close)
    {
        qDebug() << "Close";
    }
}
